package com.ruoyi.web.controller.system;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.LeaveInformation;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ILeaveInformationService;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 请假信息Controller
 * 
 * @author ruoyi
 * @date 2022-10-23
 */
@RestController
@RequestMapping("/system/information")
public class LeaveInformationController extends BaseController
{
    @Autowired
    private ILeaveInformationService leaveInformationService;
    @Autowired
    private ISysPostService postservice;
    @Autowired
    private ISysPostService sysPostService;

    /**
     * 查询请假信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:information:list')")
    @GetMapping("/list")
    public TableDataInfo list(LeaveInformation leaveInformation)
    {
    	Long useId = SecurityUtils.getLoginUser().getUser().getUserId();
    	int bs=0;
    	//查询岗位
    	List<Long> postIds = sysPostService.selectPostListByUserId(useId);
    	if(postIds!=null&&postIds.size()>0) {
    		for(Long id:postIds) {
    			 SysPost post=postservice.selectPostById(id);
    			if(post!=null&&("se".equals(post.getPostCode())||"ceo".equals(post.getPostCode()))) {
    				bs=1;
    				break;
    			}
    		}
    	}
    	if(bs==1) {
    		leaveInformation.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
    		 startPage();
    	        List<LeaveInformation> list = leaveInformationService.selectLeaveInformationList(leaveInformation);
    	        return getDataTable(list);
    	}else {
    		 return getDataTable(null);
    	}
       
    }


    /**
     * 获取请假信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:information:query')")
    @GetMapping(value = "/{lineId}")
    public AjaxResult getInfo(@PathVariable("lineId") Long lineId)
    {
        return AjaxResult.success(leaveInformationService.selectLeaveInformationByLineId(lineId));
    }

  
    /**
     * 修改请假信息
     */
    @PreAuthorize("@ss.hasPermi('system:information:edit')")
    @Log(title = "请假信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LeaveInformation leaveInformation)
    {
    	String name=SecurityUtils.getLoginUser().getUser().getUserName();
    	 leaveInformation.setUpdateBy(name);
    	 leaveInformation.setUpdateTime(new Date());
        return toAjax(leaveInformationService.updateLeaveInformation(leaveInformation));
    }
    /**
     * 新增请假信息
     */
    @PreAuthorize("@ss.hasPermi('system:information:add')")
    @Log(title = "请假信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LeaveInformation leaveInformation)
    {
    	 Long useId = SecurityUtils.getLoginUser().getUser().getUserId();
    	 leaveInformation.setUserId(useId);
    	 String name=SecurityUtils.getLoginUser().getUser().getUserName();
    	 leaveInformation.setDeletFlag("0");
    	 leaveInformation.setCreateBy(getUsername());
    	 leaveInformation.setCreateName(useId.toString());
    	 leaveInformation.setCreateTime(new Date());
    	 leaveInformation.setUserName(name);
    	 leaveInformation.setDeptId(SecurityUtils.getLoginUser().getUser().getDeptId());
    	 leaveInformation.setUpdateBy(name);
    	 leaveInformation.setUpdateTime(new Date());
    	 leaveInformation.setApprovalResults("0");
        return toAjax(leaveInformationService.insertLeaveInformation(leaveInformation));
    }

    /**
     * 删除请假信息
     */
    @PreAuthorize("@ss.hasPermi('system:information:remove')")
    @Log(title = "请假信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{lineIds}")
    public AjaxResult remove(@PathVariable Long[] lineIds)
    {
        return toAjax(leaveInformationService.deleteLeaveInformationByLineIds(lineIds));
    }
}
